<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\decorate\model\AdminProgress as progress_model;
use app\decorate\model\AdminSite;

class AdminProgress extends AdminRest{
    protected $model;
    protected $site_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model      = new progress_model();
        $this->site_model = new AdminSite();
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:进度列表
     */
    public function progressList(){
        $input = $this->_param;
        $data  = $this->model->progressList(['uniacid'=>$this->_uniacid],$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:添加进度
     */
    public function progressAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $info = $this->model->progressInfo(['title'=>$input['title'],'uniacid'=>$this->_uniacid]);
        if($info){
            return $this->error('阶段设置已存在');
        }
        $res   = $this->model->progressAdd($input);
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:编辑|回显进度
     */
    public function progressUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            if(isset($input['title'])){
                $info = $this->model->progressInfo(['title'=>$input['title'],'uniacid'=>$this->_uniacid]);
                if($info&&$info['id'] != $input['id']){
                    return $this->error('阶段设置已存在');
                }
            }
            if(isset($input['status']) && $input['status']==-1){
                $site = $this->site_model->siteInfo(['progress'=>$input['id']]);
                if(!empty($site)){
                    return $this->error('该阶段已设置工地，不可删除');
                }
            }
            $data = $this->model->progressUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->progressInfo(['id'=>$input['id']]);
        }
        return $this->success($data);
    }








}
